package com.loser.search.controller;

import cn.hutool.core.lang.UUID;
import com.loser.module.base.common.ApiResponse;
import com.loser.rpc.search.dto.req.OptLogReq;
import com.loser.rpc.search.dto.resp.OptLogResp;
import com.loser.rpc.search.fallback.SearchFeignFallBack;
import com.loser.search.entity.OptLog;
import com.loser.search.handler.DocumentHandler;
import com.loser.utils.ParamsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class OptLogController extends SearchFeignFallBack {

    @Autowired
    private DocumentHandler documentHandler;

    @Override
    public ApiResponse<Boolean> save(OptLogReq req) {

        OptLog log = ParamsUtil.copyProperties(req, OptLog.class);
        log.setId(UUID.fastUUID().toString());
        return ApiResponse.ok(documentHandler.save(log));

    }

    @Override
    public ApiResponse<List<OptLogResp>> getList(Long userId) {

        List<OptLog> logs = documentHandler.listByUserId(userId);
        return ApiResponse.ok(ParamsUtil.mapList(logs, OptLogResp.class));

    }

}
